<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Sitemap generator
 *
 * @category	Admin
 */
class SitemapAction extends Action
{

    protected $sitemapPath;
    protected $sitemapUrl;
    

    public function _initialize()
	{
		$this->sitemapPath = get_site_config('site_path') .'/sitemap.xml';
		$this->sitemapUrl  = get_site_config('site_url') .'/sitemap.xml';
	}


	//configure sitemap
	public function configure()
	{
	    $Dao = D('Config');
		if ($this->isPost())
		{
			$settings = array(
				'homepage_frequency' => $_POST['homepage_frequency'],
		        'homepage_priority'  => $_POST['homepage_priority'],
		        'category_frequency' => $_POST['category_frequency'],
		        'category_priority'  => $_POST['category_priority'],
		        'tag_frequency'      => $_POST['tag_frequency'],
		        'tag_priority'       => $_POST['tag_priority'],
		        'user_frequency'     => $_POST['user_frequency'],
		        'user_priority'      => $_POST['user_priority'],
		        'content_frequency'  => $_POST['content_frequency'],
		        'content_priority'   => $_POST['content_priority'],
		        'yahoo_appid'        => $_POST['yahoo_appid'],
		        'ping_google'        => $_POST['ping_google'],
		        'ping_yahoo'         => $_POST['ping_yahoo'],
		        'ping_bing'          => $_POST['ping_bing'],
		        'ping_ask'           => $_POST['ping_ask'],
       		);

       		$data = array('sitemap_settings' => serialize($settings));
			$Dao->updateConfigs($data);

			$filename = $this->sitemapPath;
			if (!is_writable($filename))
    		{
    		    $message = sprintf(L('sitemap.file_not_writable'), $filename);
    			$this->error($message);
    		}
			$this->generate();
			$this->ping();
			$this->success(L('sitemap.sitemap_updated_succeed'));
		}
		else
		{
		    $config = unserialize(get_site_config('sitemap_settings'));
			$priority = array('0.0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1.0');
			foreach ($priority as $value)
			{
			    $priorityOptions[$value] = $value;
			}
			$this->assign('priorityOptions', $priorityOptions);

			$frequencyOptions = array(
			    'always' => L('sitemap.always'),
			    'hourly' => L('sitemap.hour'),
			    'daily' => L('sitemap.day'),
			    'weekly' => L('sitemap.week'),
			    'monthly' => L('sitemap.month'),
			    'yearly' => L('sitemap.year'),
			    'never' => L('sitemap.never'),
			);
			$this->assign('frequencyOptions', $frequencyOptions);
			$this->assign('yahooAppID', $config['yahoo_appid']);

			$this->assign('homepagePriority', $config['homepage_priority']);
			$this->assign('categoryPriority', $config['category_priority']);
			$this->assign('tagPriority', $config['tag_priority']);
			$this->assign('userPriority', $config['user_priority']);
			$this->assign('contentPriority', $config['content_priority']);

			$this->assign('homepageFrequency', $config['homepage_frequency']);
			$this->assign('categoryFrequency', $config['category_frequency']);
			$this->assign('tagFrequency', $config['tag_frequency']);
			$this->assign('userFrequency', $config['user_frequency']);
			$this->assign('contentFrequency', $config['content_frequency']);

			$this->display();
		}
	}


	//generate sitemap.xml
	protected function generate()
	{
		import('@.Util.GoogleSitemap');

		$config  = unserialize(get_site_config('sitemap_settings'));
	    $today   = format_date();
	    $Sitemap = new google_sitemap();

	    //homepage
	    $item = new google_sitemap_item($this->siteUrl, $today, $config['homepage_frequency'], $config['homepage_priority']);
	    $Sitemap->add_item($item);

		//categories
		$Category = D('Category');
		$categories = $Category->getCategoryList();
		foreach ($categories as $value)
		{
			$categoryUrl = format_category_url($value['category_id']);
			$item = new google_sitemap_item($categoryUrl, $today, $config['category_frequency'], $config['category_priority']);
	   		$Sitemap->add_item($item);
		}

		//tags
		$Tag = D('Tag');
		$limit = 100;
		$order = array('tag_id' => 'desc');
		$tags = $Tag->order($order)->limit($limit)->select();
        foreach ($tags as $value)
		{
			$tagUrl = format_tag_url($value['tag_id']);
			$item = new google_sitemap_item($tagUrl, $today, $config['tag_frequency'], $config['tag_priority']);
	   		$Sitemap->add_item($item);
		}

		//users
		$User = D('User');
		$limit = 100;
		$order = array('user_id' => 'desc');
		$users = $User->order($order)->limit($limit)->select();
        foreach ($users as $value)
		{
			$userUrl = format_user_url($value['user_id'], $value['username']);
			$item = new google_sitemap_item($userUrl, $today, $config['user_frequency'], $config['user_priority']);
	   		$Sitemap->add_item($item);
		}

		//questions
		$Question = D('Question');
		$limit = 100;
		$where = array('is_visible' => 1);
		$order = array('question_id' => 'desc');
		$questions = $Question->where($where)->limit($limit)->select();
		foreach ($questions as $value)
		{
			$questionUrl = format_question_url($value['question_id'], $value['title']);
			$item = new google_sitemap_item($questionUrl, $today, $config['content_frequency'], $config['content_priority']);
	   		$Sitemap->add_item($item);
		}

		$filename = $this->sitemapPath;
	    $Sitemap->build($filename);
	}


	//submit sitemap to search engine
	protected function ping()
	{
	    import('ORG.Net.Http');
	    $config  = unserialize(get_site_config('sitemap_settings'));
	    $sitemap = $this->sitemapUrl;

        if (!empty($config['ping_google']))
    	{
    		$pingUrl = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='.$sitemap;
    		Http::async_request($pingUrl);
    	}
    	if (!empty($config['ping_yahoo']) && !empty($config['yahoo_appid']))
    	{
    		$pingUrl = 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid='.$config['yahoo_appid'].'&url='.$sitemap;
    		Http::async_request($pingUrl);
    	}
    	if (!empty($config['ping_bing']))
    	{
    		$pingUrl = 'http://www.bing.com/webmaster/ping.aspx?siteMap='.$sitemap;
    		Http::async_request($pingUrl);
    	}
    	if (!empty($config['ping_ask']))
    	{
    		$pingUrl = 'http://submissions.ask.com/ping?sitemap='.$sitemap;
    		Http::async_request($pingUrl);
    	}
	}


	//cron job
	public function cronjob()
	{
	    $this->generate();
	    $this->ping();
	}

}
// End  Class

/* End of file SitemapAction.class.php */
/* Location: ./App/Lib/Action/Admin/SitemapAction.class.php */